package com.red.servlet.user;

import com.red.entity.User;
import com.red.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class UserRegisterServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String account = req.getParameter("account");
        String password = req.getParameter("password");
        String email = req.getParameter("email");

        User user = new User();
        user.setuName(username);
        user.setuAccount(account);
        user.setuPassword(password);
        user.setuEmail(email);

        if (new UserServiceImpl().add(user)) {
            req.setAttribute("status", "register_success");
        } else {
            req.setAttribute("status", "register_failed");
        }
        req.getRequestDispatcher("index.jsp").forward(req, resp);
    }
}
